/**
 * 
 */
package siac.com.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import siac.com.controller.AccMoedaController;
import siac.com.entity.AccMoeda;

/**
 * @author razao
 *
 */

@FacesConverter(forClass=AccMoeda.class)
public class AccMoedaConverter implements Converter {

    @Override
	public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }
        AccMoedaController controller = (AccMoedaController)facesContext.getApplication().getELResolver().
                getValue(facesContext.getELContext(), null, "accMoedaController");
        return controller.getDao().find(getKey(value));
    }

    java.lang.Long getKey(String value) {
        java.lang.Long key;
        key = Long.valueOf(value);
        return key;
    }

    String getStringKey(java.lang.Long value) {
        StringBuffer sb = new StringBuffer();
        sb.append(value);
        return sb.toString();
    }

    @Override
	public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof AccMoeda) {
        	AccMoeda o = (AccMoeda) object;
            return getStringKey(o.getId());
        } else {
            throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: "+ AccMoeda.class.getName());
        }
    }

}